package com.briup.estore.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.estore.bean.Address;
import com.briup.estore.bean.OrderForm;
import com.briup.estore.bean.OrderLine;
import com.briup.estore.bean.ShopCarItem;
import com.briup.estore.mapper.IAddressMapper;
import com.briup.estore.mapper.IOrderMapper;
import com.briup.estore.mapper.IShopCarMapper;
import com.briup.estore.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private IShopCarMapper shopCarMapper;

	@Autowired
	private IOrderMapper orderMapper;

	@Autowired
	private IAddressMapper addressMapper;

	@Override
	public void insertOrder(Order order) {
		// TODO Auto-generated method stub
	}

	@Override
	@Transactional
	public void insertOrderList(List<BigInteger> shopCarItemIds, Address address, BigInteger customer_id) {
		System.out.println(shopCarItemIds);
		System.out.println(address);
		System.out.println("=====================================================================111111");
		if (shopCarItemIds == null || shopCarItemIds.size() == 0 || address == null) {
			return;
		}
		System.out.println("=====================================================================222222");
		if (address.getId() == null && (StringUtils.isEmpty(address.getAddress())
				|| StringUtils.isEmpty(address.getPhone()) || StringUtils.isEmpty(address.getReceivename()))) {
			return;
		}
		System.out.println("=====================================================================333333");

		OrderForm orderForm = new OrderForm();
		if (!StringUtils.isEmpty(address.getAddress()) && !StringUtils.isEmpty(address.getPhone())
				&& !StringUtils.isEmpty(address.getReceivename())) {
			address.setCustomer_id(customer_id);
			addressMapper.insertAddress(address);
		}
		orderForm.setAddress_id(address.getId());
		orderForm.setCost(new Double(0));
		orderForm.setOrderdate(new Date());
		orderForm.setCustomer_id(customer_id);
		// 新建订单
		orderMapper.newOrderForm(orderForm);
		// 新建订单项
//		System.out.println("shopCarItemIds>>" + shopCarItemIds);

		List<ShopCarItem> shopCarItems = shopCarMapper.findShopCarItemByIds(shopCarItemIds);
		double total = 0.0;
		if (shopCarItems != null && shopCarItemIds.size() > 0) {
			List<OrderLine> lines = new ArrayList<OrderLine>();
			for (ShopCarItem item : shopCarItems) {
				OrderLine line = new OrderLine();
				line.setNum(item.getNum());
				line.setCost(item.getCost());
				line.setBook_id(item.getBook_id());
				line.setOrder_id(orderForm.getId());
				lines.add(line);
				total += item.getCost();
			}
//			System.out.println("shopCarItems>>" + shopCarItems);
			for (OrderLine orderLine : lines) {
				orderMapper.insertOrderLine(orderLine);
			}

			// 更新订单值
			orderMapper.updateOrderCost(orderForm.getId(), total);
			// 删除订单项
			shopCarMapper.deleteShopCarItems(shopCarItemIds);

		}

	}

	@Override
	public List<OrderForm> findOrderFormsByCustomerId(BigInteger customer_id) {
		return orderMapper.findOrdersByCustomerId(customer_id);
	}

	@Override
	@Transactional
	public void deleteOrderById(BigInteger id) {
		// 删除订单子项
		orderMapper.deleteOrderLinesByOrderId(id);
		// 删除订单
		orderMapper.deleteOrderById(id);
	}

	@Override
	public List<OrderLine> findOrderLinesByOrderId(BigInteger order_id) {
		return orderMapper.findOrderLinesByOrderId(order_id);
	}
}
